<?php

namespace AppBundle\Form;

use AppBundle\Entity\User;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\EqualTo;

class ChangePasswordType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        /** @var  $user User*/
        $user = $builder->getData();
        $useroptions = $user->getOptions();
        $builder
            ->add('oldPassword',PasswordType::class,[
                'label'=>'原密码',
                'mapped'=>false,
                'constraints'=>[
                    new NotBlank(),
                    new EqualTo([
                        'value' => $useroptions['password'],
                        'message'=>'原密码错误',

                    ]),
                ]
            ])
            ->add('password',RepeatedType::class,[
                'type'=>PasswordType::class,
                'invalid_message'=>'重复密码不正确',
                'required'=>true,
                'first_options'=>['label'=>'密码'],
                'second_options'=>['label'=>'重复密码'],
            ])
            ->add('name',TextType::class,['label'=>'姓名'])
            ->add('mobile',TextType::class,['label'=>'手机号'])
            ->add('phone',TextType::class,['label'=>'电话'])
            ->add('email',EmailType::class,['label'=>'邮箱'])
            ->add('sex',ChoiceType::class,[
                'label'=>'性别',
                'expanded'=>'true',
                'choices'=>[
                    '男'=>'男',
                    '女'=>'女',
                ]
            ])
        ;
    }

    /**
     * @param OptionsResolver $resolver
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'AppBundle\Entity\User',
        ));
    }
}
